package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.SongSheet;
import com.lly.songhigh.utils.ChangeUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/22.
 */
public class SongSheetGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<SongSheet.DataEntity> datas;

    public SongSheetGridViewAdapter(Context context, List<SongSheet.DataEntity> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void addDatas(List<SongSheet.DataEntity> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void refreshDatas(List<SongSheet.DataEntity> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public SongSheet.DataEntity getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.song_sheet_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SongSheet.DataEntity data = getItem(position);
        if (data.getListen_count() == 0) {
            viewHolder.songSheetItemListenCount.setVisibility(View.GONE);
        } else {
            viewHolder.songSheetItemListenCount.setVisibility(View.VISIBLE);
        }
        viewHolder.songSheetItemTitle.setText(data.getTitle());
        String num = ChangeUtils.getCount(data.getListen_count());
        viewHolder.songSheetItemListenCount.setText(num);
        Picasso.with(context).load(data.getPic_url()).placeholder(R.mipmap.placeholder_disk_300)
        .error(R.mipmap.placeholder_disk_300).into(viewHolder.songSheetItemImg);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'song_sheet_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.song_sheet_item_img)
        ImageView songSheetItemImg;
        @Bind(R.id.song_sheet_item_title)
        TextView songSheetItemTitle;
        @Bind(R.id.song_sheet_item_listen_count)
        TextView songSheetItemListenCount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
